package wysiwyg.objectHandles
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import wysiwyg.shapes.LineShape;
	
	public class LineHandles extends HandleBase
	{
		public var associatedLine:LineShape;
		public var startPoint:Point;
		public var endPoint:Point;
		private var startHandle:Sprite;
		private var endHandle:Sprite;
		private var selectedHandle:Sprite;
		public function LineHandles()
		{
			super();
			percentWidth=100;
			percentHeight=100;
		}
		override protected function createChildren():void
		{
			startHandle=new Sprite();
			startHandle.graphics.beginFill(0xFF0000,.4);
			startHandle.graphics.drawCircle(0,0,5);
			startHandle.graphics.endFill();
			startHandle.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
			addChild(startHandle);
			
			
			endHandle=new Sprite();
			endHandle.graphics.beginFill(0xFF0000,.4);
			endHandle.graphics.drawCircle(0,0,5);
			endHandle.graphics.endFill();
			endHandle.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);
			addChild(endHandle);
		}
		private function handleDown(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			stage.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			selectedHandle=event.target as Sprite;	
		}
		private function handleMouseUp(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,handleMouseUp);
			handleMouseMove(event);
			selectedHandle=null;	
		}
		private function handleMouseMove(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			if(!selectedHandle)
				return;
			if(selectedHandle == startHandle)
			{
				startPoint.x=mouseX;
				startPoint.y=mouseY;
			}
			if(selectedHandle == endHandle)
			{
				endPoint.x=mouseX;
				endPoint.y=mouseY;
			}
			invalidateDisplayList();
			associatedLine.invalidateDisplayList();
		}
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			if(!startPoint||!endPoint)
				return;
			startHandle.x=startPoint.x;
			startHandle.y=startPoint.y;
			endHandle.x=endPoint.x;
			endHandle.y=endPoint.y;
		}
		
	}
}